 //
//  OAuthViewController.m
//  微博2期
//
//  Created by J on 15/11/26.
//
//

#import "OAuthViewController.h"
#import "AFNetworking.h"
#import "Account.h"
#import "AccountTool.h"
#import "HttpTool.h"//网络请求

@interface OAuthViewController ()<UIWebViewDelegate>

@end

@implementation OAuthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //1. 创建一个webView
    UIWebView * web = [[UIWebView alloc]initWithFrame:self.view.bounds];
    web.delegate = self;
    [self.view addSubview:web];
    
    //2. 用webView加载登陆界面  redirect_uri默认值为 http://     两个必选参数 redirect_uri  client_id 显示为谁想访问你的微博帐号
    
    NSString* urlStr = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=http://pan.baidu.com/disk/home",AppKey];
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [web loadRequest:request];
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [MBProgressHUD showMessage:@"正在加载..."];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [MBProgressHUD hideHUD];
    
}

- (void)webViewDidFaildLoad:(UIWebView *)webView{
    
    [MBProgressHUD hideHUD];

    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSString * url = request.URL.absoluteString;
    
    //判断是否为回调地址
    NSRange range = [url rangeOfString:@"code="];
    if (range.length!=0) {//是回调地址
        //截取code= 后面的参数值
      NSUInteger fromIndex = range.location + range.length;
      NSString* code =   [url substringFromIndex:fromIndex];
        
        //利用code换取一个accessToken
        [self accessTokenWithCode:code];
        
        return NO;//授权成功后禁止加载回调地址
        
    }
    
//    NSLog(@"%@",request.URL.absoluteString);
    return YES;
    
}

/**
 *  利用code (授权成功后的request token) 换取accessToken
 *
 *  @param code 授权成功后的request token
 */

- (void)accessTokenWithCode:(NSString*)code{
    
    //1.请求管理者
//    AFHTTPRequestOperationManager * manager= [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer =  [AFJSONResponseSerializer serializer];
    //2.拼接请求参数
    
    
    //2.发送请求
//    https://api.weibo.com/oauth2/access_token?client_id=3920546846&client_secret=5cbe2134a99ad4bc4955f420d55ac7f5&grant_type=authorization_code&redirect_uri=http://pan.baidu.com/disk/home
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"client_id"]= AppKey ;
    params[@"client_secret"]=APPSecret ;
    params[@"grant_type"]=@"authorization_code";//authorization_code固定
    params[@"code"]=code;//包含用户帐号信息
    params[@"redirect_uri"]=RedirectURL;

    
     [HttpTool post:@"https://api.weibo.com/oauth2/access_token" params:params success:^(NSDictionary* json) {
         [MBProgressHUD hideHUD];
         //        NSLog(@"%@",responseObject);
         //将返回的帐号字典数据->模型, 存进沙盒
         //沙盒路径
         
         Account * account = [Account accountWithDic:json];//字典转模型
         [AccountTool saveAccount:account];//存储封装
         UIWindow * window = [UIApplication sharedApplication].keyWindow;
         [window switchRootViewController];
         //UIWindow分类中 WindowToll UIViewController分类 UIViewControllerTool

     } fail:^(NSError *error) {
         [MBProgressHUD hideHUD];
         NSLog(@"%@",error);
         
     }];
     
     
    
    
//    {
//        "access_token" = "2.00DoTN5DY1N1RE2b4278b2c4SQjbFD";//一个用户给一个应用授权成功后 就会获得对应的一个accessToken  3个不同用户给4个不同的应用授权成功后 会获得12个不同的accessToken
//        "expires_in" = 157679999;
//        "remind_in" = 157679999;
//        uid = 3106243403;//userid 一个用户对应一个uid 一个用户和一个应用对应一个access_token
//    }
    
}



@end
